package struts;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bisness.PandAction;
import service.vo.MessageVo;
import service.vo.XMLStruts.XMLBean;

import struts.form.ActionForm;
import struts.form.FullForm;

public class ActionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public ActionServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("name");
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		String servletPath= request.getServletPath();
		String questHead = this.getServerPath(servletPath);
		Map<String, XMLBean> map = (Map<String, XMLBean>) this
				.getServletContext().getAttribute("struts");
		XMLBean xml = map.get(questHead);
		String formClass = xml.getFormClass();
		ActionForm form = FullForm.full(formClass, request);
		String actionType = xml.getActionClass();
		Action action = null;
		String url ="";
		try {
			Class clazz = Class.forName(actionType);
			action = (Action) clazz.newInstance();
			 url = action.execute(request, form, xml.getActionForward());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			System.out.println("控制器出现异常");
		}
		//定位到指定url 路径的页面
		RequestDispatcher  dis = request.getRequestDispatcher(url);
		dis.forward(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

	public String getServerPath(String str) {

		return str.split("\\.")[0];
	}

}
